from __future__ import annotations

from dataclasses import dataclass
from math import pi


@dataclass
class BraceCheck:
    slenderness: float
    euler_capacity_kN: float
    utilization: float
    ok: bool
    note: str


def check_brace_stability(length_m: float, radius_gyration_mm: float, axial_kN: float, elastic_modulus_MPa: float = 200000.0, end_condition_factor: float = 1.0) -> BraceCheck:
    """内支撑压杆稳定：欧拉屈曲近似与长细比控制。"""

    L_eff = length_m * end_condition_factor
    r = max(1e-6, radius_gyration_mm) / 1000.0
    slenderness = (L_eff) / max(1e-6, r)
    I_over_A = r ** 2
    # 取截面面积未知，等效用 r^2A => Pcr ~ pi^2 E I / (L^2)；此处以单位面积近似
    E = elastic_modulus_MPa * 1e6
    Pcr = (pi ** 2) * E * I_over_A / max(1e-9, (L_eff ** 2)) / 1000.0  # kN/单位面积
    # 用经验安全系数将Pcr缩放，给出利用率趋势
    utilization = axial_kN / max(1e-6, 0.6 * Pcr)
    ok = utilization <= 1.0 and slenderness <= 180.0
    return BraceCheck(slenderness=slenderness, euler_capacity_kN=0.6 * Pcr, utilization=utilization, ok=ok, note="简化欧拉+长细比限值")


